﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Web;
using mojoPortal.Business.WebHelpers;

namespace blackhouse.WebNemo.Cms {
    public class EditRecordLink : HyperLink {

        private IGroup group;
        private int recordId;
        private Factory factory = Factory.Instance();
        private Dictionary<string, string> addUrlParams = new Dictionary<string, string>();

        public override bool Visible {
            get {
                return base.Visible && (WebUser.IsAdminOrContentAdminOrContentPublisherOrContentAuthor || WebUser.IsInRole("nemo"));
            }
            set {
                base.Visible = value;
            }
        }

        public int GroupId {
            get {
                if (group != null)
                    return this.group.Id;
                else
                    return 0;
            }
            set {
                if (group == null || this.group.Id != value) {
                    this.group = this.factory.GetGroup(value);
                    this.LoadForGroup();
                }
            }
        }

        public int RecordId {
            get {
                return recordId;
            }
            set {
                if (this.recordId != value) {
                    this.recordId = value;
                    this.LoadForGroup();
                }
            }
        }

        public bool PreventChangeText { get; set; }

        public string SystemGroupName {
            get {
                if (group != null)
                    return this.group.SystemName;
                else
                    return "";
            }
            set {
                if (group == null || this.group.SystemName != value) {
                    this.group = this.factory.GetGroup(value);
                    this.LoadForGroup();
                }
            }
        }

        public void AddUrlParameter(string key, string parameterValue) {
            if (this.addUrlParams.ContainsKey(key))
                this.addUrlParams[key] = parameterValue;
            else
                this.addUrlParams.Add(key, parameterValue);
        }

        public void AddUrlParameters(Dictionary<string, string> parameters) {
            foreach (KeyValuePair<string, string> item in parameters) {
                this.AddUrlParameter(item.Key, item.Value);
            }
        }

        protected void LoadForGroup() {
            if (group == null) {
                this.Visible = false;
                return;
            }
            if (!this.PreventChangeText) {
                this.Text = "Edytuj rekord (" + this.group.Name + ")";
            }
            this.NavigateUrl = NemoUrlUtils.GetUrlUtils().GetEditRecord(this.group.Id, this.RecordId, NemoUrlUtils.DisplayModFullWidth) + "&returnUrl=" + HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl);
            foreach (KeyValuePair<string, string> urlItem in this.addUrlParams) {
                this.NavigateUrl += string.Format("&{0}={1}", urlItem.Key, HttpUtility.UrlEncode(urlItem.Value));
            }
        }

    }
}